﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WhittleList.Bll;

namespace WhittleList.Helpers
{
    public static class UrlHelpers
    {
        /// <summary>
        /// Returns app based absolute path for given image path relative to ~/Context/Images/
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        
        public static string Image(this UrlHelper url, string fileName)
        {
            //<img src="<%=Url.Image("Image1.gif")%>"/> results in html <img src="/Context/Images/Image1.gif"/>
            //Image1.gif returns resolved url /Context/Images/Image1.gif
            return url.Content(string.Concat("~/Context/Images/", fileName));
        }
 
        /// <summary>
        /// Returns app based absolute path for given stylesheet path relative to ~/Context/StyleSheets/
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string Stylesheet(this UrlHelper url, string fileName)
        {
            return url.Content(string.Concat("~/Content/StyleSheets/", fileName));
        }

        /// <summary>
        /// Returns app based absolute path for given script path relative to ~/Scripts/
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string Javascript(this UrlHelper url, string fileName)
        {
            return url.Content(string.Concat("~/Scripts/", fileName));
        }


        public static string Home(this UrlHelper url)
        {
            return url.Action("Index", "Home");
        }

        public static string Site(this UrlHelper url, Site site, string action)
        {
            return url.Site(site, action, url.RequestContext.RouteData.GetRequiredString("controller"), null);
        }

        public static string Site(this UrlHelper url, Site site, string action, string controller)
        {
            return url.Site(site, action, controller, null);
        }

        public static string Site(this UrlHelper url, Site site, string action, string controller, object routeValues)
        {
            var relUrl = url.Action(action, controller, routeValues);

            var split = url.RequestContext.HttpContext.Request.Headers["HOST"].Split(':');
            var port = split.Count() > 1 ? string.Concat(":", split[1]) : "";
            var currentUrl = split[0];
            
            if (currentUrl == site.Url)
            {
                return relUrl;
            }
            else if(currentUrl != "localhost" && currentUrl != "www.whittledev.com")
            {
                var domainSplit = split[0].Split('.');
                var slug = domainSplit[0];
                if (slug == "www")
                    slug = domainSplit[1];
                
                if (slug == site.Url)
                    return relUrl;
            }

            //got here so return full url
            return GetFullSiteUrl(site, port, relUrl);
        }

        private static string GetFullSiteUrl(Site site, string port, string relUrl)
        {
            if (site.Url.Contains("."))
                return string.Concat(site.Url, port, relUrl);
            else
                return string.Format("http://www.{0}.whittledev.com{1}{2}", site.Url, port, relUrl);
        }

        public static string Dashboard(this UrlHelper url, Site site)
        {
            return url.Site(site, "Main", "Dashboard");
        }

        public static string GetPotentialUrl(this HttpRequestBase request)
        {
            var hostHeader = request.Headers["HOST"].Split(':')[0];

            if (hostHeader == "localhost" || hostHeader == "www.whittledev.com")
            {
                return null;
            }
            else if(hostHeader.EndsWith(".whittledev.com"))
            {
                var split = hostHeader.Split('.');
                if (split[0] == "www")
                    return split[1];
                else
                    return split[0];
            }
            else
            {
                return hostHeader;
            }
        }

        public static string Project(this UrlHelper url, Project project, string action)
        {
            return url.Action(action, "Project", new { id = project.Id, siteId = project.SiteId });
        }
    }
}
